package theory.vanpelt.bifurcation;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 

import java.math.BigInteger;

/**
 *  Memoizing factorialisation. Pre-computed n! values for up to 101!. 
 *
 * @author risto
 */
public class Fact {

	/**
	 * Pre-computed array of factorial values.
	 */
    public static BigInteger fact[] = new BigInteger[1000];

    /**
     * Compute n!
     * @param a
     * @return
     */
    public static BigInteger fact(int a)
    {
        if (a < 2) return BigInteger.ONE;
        if (fact[a] != null) return fact[a];

        BigInteger n = BigInteger.ONE;
        for (int i=1; i<=a; i++) {
            n = n.multiply(BigInteger.valueOf(i));
            fact[i] = n;
        }
        return n;
    }

    /*
     * Pre-compute up to 101!
     */
    static {
        fact[0] = BigInteger.ONE;
        fact[1] = BigInteger.ONE;
        fact(501);
    }
}
